//
//  YTBallViewController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/9.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTBallViewController.h"
#import "FRSphereView.h"
#import "YTRankViewController.h"
#import "YTETAIShopController.h"
#import "YTEtaiOrieButton.h"

@interface YTBallViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *rankImgView;
@property(nonatomic ,strong)UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UIButton *suanliButton;

@property (nonatomic,strong) FRSphereView *sphereView; // 球型标签View
@property(nonatomic ,strong)NSMutableArray *arrayBtn;

@property (weak, nonatomic) IBOutlet UIButton *tatolEtaiButton; // ETAI 总数
@property (weak, nonatomic) IBOutlet UILabel *calcPowerLabel;   // 算力值
@property (weak, nonatomic) IBOutlet UILabel *rankLabel;    // 排行榜
@property (weak, nonatomic) IBOutlet UIImageView *growingUPImageView;

@property(nonatomic ,strong)YTEtaiModel *model;
@end

@implementation YTBallViewController


- (void)setModel:(YTEtaiModel *)model
{
    _model = model;
    
    [self.tatolEtaiButton setTitle:[NSString stringWithFormat:@"%@ ETAI",model.total_money] forState:UIControlStateNormal];
    self.calcPowerLabel.text = [NSString stringWithFormat:@"算力值: %@",model.total_etai];
    self.rankLabel.text = [NSString stringWithFormat:@"ETAI排名: %@",model.ranking];
    
    [model.data_one enumerateObjectsUsingBlock:^(YTEtaiOre * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YTEtaiOrieButton *btn = [[YTEtaiOrieButton alloc] init];
        [btn setTitle:[NSString stringWithFormat:@"%@ EATI", obj.etai_num] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ball_baoshi"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.frame = CGRectMake(0, 0, 80, 80);
        btn.layer.cornerRadius = 3;
        btn.clipsToBounds = YES;
        btn.tag = 999 + idx;
        [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.arrayBtn addObject:btn];
        [self.sphereView addSubview:btn];
        
    }];
    
    [self.sphereView setItems:self.arrayBtn animation:true];
    
    self.growingUPImageView.hidden = (model.data_one.count == 0) ? false : YES;
    
}

#pragma mark - lazy sphereView
- (FRSphereView *)sphereView {
    if (!_sphereView) {
        CGFloat sphereViewW = kScreenWidth - 50 * 2;
        CGFloat sphereViewH = sphereViewW;
        
        CGFloat sphereViewY = iPhone5 ? self.suanliButton.frame.origin.y - sphereViewH - 50 : self.suanliButton.frame.origin.y - sphereViewH + 50;
        _sphereView = [[FRSphereView alloc] initWithFrame:CGRectMake(30, sphereViewY, sphereViewW, sphereViewH)];
        
        CGPoint center = _sphereView.center;
        center.x = self.view.center.x;
        _sphereView.center = center;
    }
    return _sphereView;
}


- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _bgImageView.image = [UIImage imageNamed:@"bj_Etaiball"];
    }
    return _bgImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];

    self.arrayBtn = [NSMutableArray array];

    [self.view addSubview:self.sphereView];
    
    
    [self loadData];
}

- (void)loadData
{
    [YTNetworkManager.shareManager requestWithBallDataWithCompletion:^(YTEtaiModel *model, BOOL isSucc, NSString *error) {
        if (!isSucc) {
            [self.view showAwardWithTipStr:error];
            return ;
        }
        
        self.model = model;
    }];
}

#pragma mark - 矿 点击
- (void)buttonPressed:(YTEtaiOrieButton *)sender
{
    [self.sphereView timerStop];
    sender.enabled = false;
    [UIView animateWithDuration:0.3 animations:^{
        sender.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{

            [YTNetworkManager.shareManager requestETAIWithEid:self.model.data_one[sender.tag - 999].eid
                                                   completion:^(id json, BOOL isSucc, NSString *error)
            {
                if (!isSucc) {
                    [self.view showErrorWithTipStr:error];
                    return ;
                }
                self.model.total_etai = json[@"data"][@"total_etai"];
                self.model.total_money = json[@"data"][@"total_money"];
                self.model.ranking = json[@"data"][@"ranking"];
             
                
                [self.tatolEtaiButton setTitle:[NSString stringWithFormat:@"%@ ETAI",self.model.total_money] forState:UIControlStateNormal];
                self.calcPowerLabel.text = [NSString stringWithFormat:@"算力值: %@",self.model.total_etai];
                self.rankLabel.text = [NSString stringWithFormat:@"ETAI排名: %@",self.model.ranking];
                
                if ([json[@"data"][@"add_dig"] isKindOfClass:[NSNull class]]) {
                    sender.hidden = YES;
                    [self.sphereView timerStart];

                    NSArray *array = [self.sphereView.subviews valueForKey:@"isHidden"];
                    NSArray *boolList = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
                        return [evaluatedObject boolValue] == YES;
                    }]];
                    
                    self.growingUPImageView.hidden = (self.sphereView.subviews.count != boolList.count);
                    
                    return;
                }
                
         
                [self.model.data_one replaceObjectAtIndex:(sender.tag - 999) withObject:[YTEtaiOre yy_modelWithJSON:json[@"data"][@"add_dig"]]];
                sender.transform = CGAffineTransformMakeScale(1., 1.);
                sender.hidden = YES;
                
                YTEtaiOrieButton *btn = [[YTEtaiOrieButton alloc] init];
                [btn setTitle:[NSString stringWithFormat:@"%@ EATI", json[@"data"][@"add_dig"][@"etai_num"]] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"ball_baoshi"] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
                btn.tag = sender.tag;
                btn.layer.cornerRadius = 3;
                btn.frame = CGRectMake(0, 0, 80, 80);
                btn.clipsToBounds = YES;

                [self.arrayBtn replaceObjectAtIndex:sender.tag - 999 withObject:btn];
                [self.sphereView addSubview:btn];
                [self.sphereView setItems:self.arrayBtn animation:true];
                
                
            }];
           
        } completion:^(BOOL finished) {
           // [self.sphereView timerStart];
            
        }];
    }];
    
               // sender.transform = CGAffineTransformMakeScale(1., 1.);

    

}
#pragma mark: - 精彩活动 按钮监听
- (IBAction)moreActionButtonClick
{
    YTMineWebController *webVC = [[YTMineWebController alloc] init];
    webVC.title = @"精彩活动";
    // FIXME: - 记得修改成正式环境
    webVC.isActive = YES;
    webVC.url = [NSString stringWithFormat:@"%@v1/TestQuestions/activityList",kMainUrlStr];
    [self.navigationController pushViewController:webVC animated:true];

}
#pragma mark: - 以太商城按钮 监听
- (IBAction)etaiShopButtonClick
{
    YTMineWebController *webVC = [[YTMineWebController alloc] init];
    webVC.shouldLogin = YES;
    webVC.title = @"星域宝藏";
    // FIXME: - 记得修改成正式环境
    webVC.url = [NSString stringWithFormat:@"%@InvitingFriends/etaiAssets",kMainUrlStr];
    [self.navigationController pushViewController:webVC animated:true];
}

#pragma mark: - 漫游指南 按钮监听
- (IBAction)companionButtonClick {
    YTMineWebController *webVC = [[YTMineWebController alloc] init];
    
    // FIXME: - 记得修改正式环境的地方
    webVC.url = [NSString stringWithFormat:@"%@InvitingFriends/etaiStar",kMainUrlStr];
    [self.navigationController pushViewController:webVC animated:true];
}
#pragma mark: - 设置界面
- (void)setupUI
{
    self.view.backgroundColor = UIColor.randomColor;

    // setNav
    // 透明
    [self showNavigationWithClearBGWIthRect:CGRectMake(0, 0, 1, 1) alpha:0];
    
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui_EtaiBall"]
                                                                style:UIBarButtonItemStylePlain
                                                               target:self
                                                               action:@selector(backButtonClick)];
//    UIBarButtonItem *soudBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"形状10"]
//                                                                style:UIBarButtonItemStylePlain
//                                                               target:nil action:nil];
    
    self.navigationItem.leftBarButtonItem = backBtn;
    // 设置背景图
    [self.view insertSubview:self.bgImageView atIndex:0];
    
    self.navigationItem.title = @"以太星域";
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
    
}
#pragma mark: - 排行榜按钮 点击
- (IBAction)rankButtonClick:(UIButton *)sender
{
    YTRankViewController *rankVC = [[YTRankViewController alloc] init];
    [self.navigationController pushViewController:rankVC animated:true];
}

- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showNavigationWithClearBGWIthRect:CGRectMake(0, 0, 1, 1) alpha:0];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
    [self.navigationController.navigationBar setTintColor:UIColor.whiteColor];

    
    [MobClick beginLogPageView:@"以太星域"];

}
#pragma mark: - viewWillDisappear
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self showNavigationWithClearBGWIthRect:self.navigationController.navigationBar.bounds alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.blackColor}];

    [MobClick endLogPageView:@"以太星域"];
}

#pragma mark: - prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinVC = segue.destinationViewController;
    if ([segue.identifier isEqualToString:@"go2ETAIShop"]) {
        [(YTETAIShopController *)destinVC setEtaiTotal:self.model.total_money];
    }
}


#pragma mark: - 导航栏透明方法
- (void)showNavigationWithClearBGWIthRect:(CGRect)rect alpha:(CGFloat)alpha{
    
    self.navigationController.navigationBar.translucent = (rect.size.width > 1) ? NO : YES;
    
    // 将状态栏和导航条设置成透明
    UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:alpha] rect:rect];
    
    [self.navigationController.navigationBar setBackgroundImage: (rect.size.width > 1) ? nil : image forBarMetrics:UIBarMetricsDefault];
}


@end
